package fr.ece.ing4.si.montercarlogui;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.UIManager;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import fr.ece.ing4.si.montecarlo.monteCarlo;
import fr.ece.ing4.si.montecarlo.monteCarloThread;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.text.*;
import java.util.concurrent.ExecutionException;
import java.awt.Color;

public class gui extends JFrame{

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField textField_9;
	
	private double s;
	private double x;
	private double t;
	private double r;
	private double b;
	private double v;
	private int nSteps;
	private int nSimulations;
	private String flag = "c";
	private String flag2 = "s";
	private double result;
		
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					gui frame = new gui();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/* Create the frame. */
	public gui() {
		setResizable(false);
		setTitle("MonteCarlo");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 420, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		//Action for the Buttom: GO
		JButton btnGo = new JButton("Go");
		btnGo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				try {
					
					//Read variables from the text fields.
					s = Double.parseDouble(textField.getText());
					x = Double.parseDouble(textField_1.getText());
					t = Double.parseDouble(textField_2.getText());
					r = Double.parseDouble(textField_3.getText());
					b = Double.parseDouble(textField_4.getText());
					v = Double.parseDouble(textField_5.getText());	
					nSteps = Integer.parseInt(textField_6.getText());
					nSimulations = Integer.parseInt(textField_7.getText());
					
					//Get current time
					final long start = System.nanoTime();
					
					//Check if CALL or PUT
					if(flag2 =="c"){
						monteCarloThread m = new monteCarloThread(flag, s, x, t, r, b, v, nSteps, nSimulations);
						result = m.computePremium();
					}
					else if(flag2 =="s"){
						monteCarlo m2 = new monteCarlo(flag, s, x, t, r, b, v, nSteps, nSimulations);
						result = m2.computePremium();
					}
					
					DecimalFormat V1 = new DecimalFormat("##.#####");
					DecimalFormat V2 = new DecimalFormat("##.#####");
					String z= String.valueOf(V1.format(result));
					
					//Get ending time of the process
					final long end = System.nanoTime();
					double time = (end - start)/1.0e9;
					String Time = String.valueOf(V2.format(time));
					
					//Show result and time
					textField_9.setText(Time);
					textField_8.setText(z);
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		
		//Window elements creation
		JLabel lblS = new JLabel("S:");
		textField = new JTextField();
		textField.setColumns(10);
		
		JLabel lblX = new JLabel("X:");
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		
		JLabel lblT = new JLabel("T:");
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		
		JLabel lblR = new JLabel("r:");
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		
		
		JLabel lblB = new JLabel("b:");
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		
		JLabel lblV = new JLabel("v:");
		textField_5 = new JTextField();
		textField_5.setColumns(10);
		
		JLabel lblNsteps = new JLabel("nSteps");
		textField_6 = new JTextField();
		textField_6.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("nSimulations");
		textField_7 = new JTextField();
		textField_7.setColumns(10);
		
		final JRadioButton rdbtnBuy = new JRadioButton("Buy");
		final JRadioButton rdbtnSell = new JRadioButton("Sell");
		
		//Control Radio Button Buy/Sell
		rdbtnSell.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					rdbtnSell.setSelected(true);
					rdbtnBuy.setSelected(false);
					flag = "p";
			}
		});
		
		rdbtnBuy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					rdbtnSell.setSelected(false);
					rdbtnBuy.setSelected(true);
					flag = "c";
			}
		});
		
		ButtonGroup CallPut = new ButtonGroup();
	    CallPut.add(rdbtnBuy);
	    CallPut.add(rdbtnSell);
	    rdbtnBuy.setSelected(true);
		
		JLabel lblPremium = new JLabel("PREMIUM:");
		textField_8 = new JTextField();
		textField_8.setColumns(10);
		
		JLabel lblPorcessTime = new JLabel("Porcess Time (seg):");
		textField_9 = new JTextField();
		textField_9.setColumns(10);
		
		//Action for the Button LOAD PARAMS
		JButton btnLoadParams = new JButton("Load Params");
		btnLoadParams.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String[] Params = new String[6];
				try {
					//Any of the classes (sequential or concurrent have the same method to load params
					Params = monteCarloThread.readParameters();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				//Show Params loaded in the text fields
				textField.setText(Params[0]);
				textField_1.setText(Params[1]);
				textField_2.setText(Params[2]);
				textField_3.setText(Params[3]);
				textField_4.setText(Params[4]);
				textField_5.setText(Params[5]);
			}
		});
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.LIGHT_GRAY);
		
		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
									.addGroup(gl_contentPane.createSequentialGroup()
										.addComponent(lblR)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))
									.addGroup(gl_contentPane.createSequentialGroup()
										.addComponent(lblB)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(textField_4, 0, 0, Short.MAX_VALUE))
									.addGroup(gl_contentPane.createSequentialGroup()
										.addComponent(lblV)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(textField_5, 0, 0, Short.MAX_VALUE)))
								.addComponent(lblPremium)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
									.addGroup(gl_contentPane.createSequentialGroup()
										.addComponent(lblT)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(textField_2, 0, 0, Short.MAX_VALUE))
									.addGroup(gl_contentPane.createSequentialGroup()
										.addComponent(lblX)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(textField_1, 0, 0, Short.MAX_VALUE))
									.addGroup(gl_contentPane.createSequentialGroup()
										.addComponent(lblS)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(textField, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE))))
							.addGap(14))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblPorcessTime)
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(18)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(24)
									.addComponent(lblNsteps)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(textField_6, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(lblNewLabel)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(textField_7, 0, 0, Short.MAX_VALUE))
								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
									.addComponent(rdbtnBuy)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
										.addComponent(rdbtnSell)
										.addGroup(gl_contentPane.createSequentialGroup()
											.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
												.addComponent(textField_9, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
												.addComponent(textField_8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
											.addGap(7)))
									.addComponent(btnLoadParams))))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(69)
							.addComponent(btnGo)))
					.addGap(31)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
					.addGap(2))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblS)
										.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblX)
										.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
										.addComponent(textField_6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblNsteps))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblNewLabel)
										.addComponent(textField_7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblT)
								.addComponent(rdbtnBuy)
								.addComponent(rdbtnSell))
							.addPreferredGap(ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblR)
										.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblB)
										.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addComponent(lblV)
										.addComponent(textField_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(4)
									.addComponent(btnLoadParams)
									.addGap(18)
									.addComponent(btnGo)))
							.addGap(18)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(6)
									.addComponent(lblPremium))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(textField_8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblPorcessTime)
								.addComponent(textField_9, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(41))))
		);
		
		JLabel lblThreading = new JLabel("Threading");
		panel.add(lblThreading);
		
		final JRadioButton rdbtnConcurrent = new JRadioButton("Concurrent");
		final JRadioButton rdbtnSequential = new JRadioButton("Sequential");
		
		//Control the radio button for the the type of process
		rdbtnConcurrent.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				rdbtnSequential.setSelected(false);
				rdbtnConcurrent.setSelected(true);
				flag2 = "c";
			}
		});
		
		rdbtnConcurrent.setBackground(Color.LIGHT_GRAY);
		panel.add(rdbtnConcurrent);
		
		rdbtnSequential.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				rdbtnSequential.setSelected(true);
				rdbtnConcurrent.setSelected(false);
				flag2 = "s";
			}
		});
		rdbtnSequential.setBackground(Color.LIGHT_GRAY);
		rdbtnSequential.setSelected(true);
		panel.add(rdbtnSequential);
		contentPane.setLayout(gl_contentPane);
	}

}
